import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Param,
  Post,
  Put
} from '@midwayjs/core'
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags
} from '@midwayjs/swagger'
import { Validate } from '@midwayjs/validate'

import { Permission } from '@/decorator'
import { DeleteDTO } from '@/dto'

import { CreateDeptDTO, FindDeptListDTO, UpdateDeptDTO } from './dept.dto'
import { DeptService } from './dept.service'
import { GetDeptListVO, GetDeptTreeVO } from './dept.vo'

@ApiBearerAuth()
@ApiTags('系统/部门接口')
@Controller('/system/dept')
export class DeptController {
  @Inject()
  deptService: DeptService

  @ApiOperation({ summary: '获取部门列表' })
  @ApiResponse({ status: 200, type: GetDeptListVO })
  @Permission('system:dept:list')
  @Post('/list')
  @Validate()
  async getDeptList(@Body() dto: FindDeptListDTO) {
    return await this.deptService.handleFindList(dto)
  }

  @ApiOperation({ summary: '获取部门树' })
  @ApiResponse({ status: 200, type: GetDeptTreeVO })
  @Get('/tree')
  async getDeptTree() {
    return this.deptService.handleFindTree()
  }

  @ApiOperation({ summary: '新建部门' })
  @Permission('system:dept:add')
  @Post('/create')
  @Validate()
  async createDept(@Body() dto: CreateDeptDTO) {
    await this.deptService.handleCreate(dto)
  }

  @ApiOperation({ summary: '更新部门' })
  @Permission('system:dept:edit')
  @Put('/update/:id')
  @Validate()
  async updateDept(@Param('id') id: string, @Body() dto: UpdateDeptDTO) {
    await this.deptService.handleUpdate(id, dto)
  }

  @ApiOperation({ summary: '删除部门' })
  @Permission('system:dept:remove')
  @Del('/delete')
  @Validate()
  async deleteDept(@Body() dto: DeleteDTO) {
    await this.deptService.handleDelete(dto)
  }
}
